package com.android.kmlmrg.services;

import java.util.List;

import android.app.Activity;
import android.app.Application;
import android.app.ProgressDialog;
import android.content.Intent;
import android.util.Log;
import android.widget.Toast;

import com.android.kmlmrg.event.RestAddUserEvent;
import com.android.kmlmrg.event.RestGetAllUserEvent;
import com.android.kmlmrg.event.RestGetUserEvent;
import com.android.kmlmrg.event.RestLoginEvent;
import com.android.kmlmrg.event.RestReportEvent;
import com.android.kmlmrg.event.UserDelEvent;
import com.android.kmlmrg.listener.custom.RestAddUserListener;
import com.android.kmlmrg.listener.custom.RestGetAllUserListener;
import com.android.kmlmrg.listener.custom.RestGetUserListener;
import com.android.kmlmrg.listener.custom.RestLoginListener;
import com.android.kmlmrg.listener.custom.RestReportListener;
import com.android.kmlmrg.listener.custom.RestUserDelListener;
import com.android.kmlmrg.model.ApiSession;
import com.android.kmlmrg.model.User;
import com.android.kmlmrg.model.Users;
import com.android.kmlmrg.services.rest.method.AddUser;
import com.android.kmlmrg.services.rest.method.DeleteUser;
import com.android.kmlmrg.services.rest.method.GetUser;
import com.android.kmlmrg.services.rest.method.Login;
import com.android.kmlmrg.services.rest.method.Logout;
import com.android.kmlmrg.services.rest.method.Photo;
import com.android.kmlmrg.services.rest.method.Report;
import com.android.kmlmrg.services.rest.method.UpdateUser;
import com.android.kmlmrg.services.rest.method.UpdateUserPhoto;
import com.android.kmlmrg.services.rest.method.UsersRest;
import com.android.kmlmrg.utils.MethodeFile;
import com.android.kmlmrg.utils.MethodeUtils;
import com.android.kmlmrg.views.DashBoard;
import com.android.kmlmrg.views.FormAuthentication;
import com.android.kmlmrg.views.ViewContact;

public class ServiceController implements 
RestGetAllUserListener, 
RestGetUserListener,
RestLoginListener,
RestAddUserListener{

	/**LOG**/
	private static final  String LOG_TAG = ServiceController.class.getName();

	private static ServiceController serviceController = null;

	private static ApiSession apiSession =null;

	private Application application = null ;

	private static String login = null;

	private static String password = null;

	private Users users = null;

	private ProgressDialog progress;

	public static int LOGIN_ID = 1;

	private static boolean isAuthentication = false;



	private ServiceController ( Application pApplication ) {
		Log.i(LOG_TAG, "START - constructeur ServiceController");
		this.application = pApplication;	
		users = new Users();
		Login.addListener(this);
		AddUser.addListener(this);
		UsersRest.addListener(this);
		GetUser.addListener(this);


		Log.i(LOG_TAG, "END - constructeur ServiceController");

	}



	public static  ServiceController getInstance ( Application pApplication, String pLogin, String pPassword ) {
		if ( serviceController == null ) {			
			serviceController = new ServiceController( pApplication );

		}	
		serviceController.login (pLogin, pPassword,  false);
		return serviceController;
	}


	public void login (String pLogin, String pPassword, boolean pIsFromFormView) {
		Log.i(LOG_TAG, "START - login  ");

	
		login = pLogin;
		password = pPassword;
		Login vLogin = new Login(application, pLogin, pPassword, pIsFromFormView) ;
		vLogin.execute();

		Log.i(LOG_TAG, "END - login  ");
	}



	public synchronized static  ServiceController getInstance (  ) {

		return serviceController;
	}



	public synchronized void updateUserPhoto (User pUser, String pPathImg) {
		Log.i(LOG_TAG, "START - updateUserPhoto  ");
		String vStringStream = MethodeFile.getStringByteJpegFromFile( pPathImg );
		UpdateUserPhoto vUpdateUserPhoto = new UpdateUserPhoto(getApplication(), pUser, vStringStream);
		vUpdateUserPhoto.execute();
		Log.i(LOG_TAG, "END - updateUserPhoto  ");
	}


	//	public void addUserServ (User pUser, Activity pActivity) {
	//		Log.i(LOG_TAG, "START - AddUser (User pUser) ");
	//	
	//		AddUser vAddUser = new AddUser(application, pUser);
	//		
	//		vAddUser.execute();		
	//		Log.i(LOG_TAG, "END - AddUser  ");			
	//	}

	public synchronized void addUserServ (User pUser) {
		Log.i(LOG_TAG, "START - AddUser (User pUser) ");
		//		pUser.setInfoSession(apiSession);
		AddUser vAddUser = new AddUser(application, pUser);
		vAddUser.execute();		
		Log.i(LOG_TAG, "END - AddUser  ");			
	}

	public synchronized void printReport () {
		Log.i(LOG_TAG, "START - printReport () ");
		Report vReport = new Report(application);
		vReport.execute();
		Log.i(LOG_TAG, "END - printReport () ");
	}

	public synchronized void getAllUsersByRest () {
		Log.i(LOG_TAG, "START - getAllUsers ");
		UsersRest vUserRest = new UsersRest(application);
		vUserRest.execute();
		Log.i(LOG_TAG, "END - getAllUsers ");
	}

	public synchronized void deleteUser(User pUser){
		Log.i(LOG_TAG, "START -  deleteUser(User pUser) ");
		if(pUser!=null){
			DeleteUser vDeleteUser = new DeleteUser(application,pUser );
			vDeleteUser.execute();
		}
		else{
			Log.i(LOG_TAG, "Error -  deleteUser(User pUser) - pUser is null ");
		}
		Log.i(LOG_TAG, "START -  deleteUser(User pUser) ");
	}

	public synchronized void logout(){
		Log.i(LOG_TAG, "START -  logout() ");
		Logout vLogout = new Logout(application);
		vLogout.execute();
		Log.i(LOG_TAG, "END -  logout() ");
	}

	public synchronized void updateUserServ(User pUser ){
		Log.i(LOG_TAG, "START -  updateUserServ(User pUser) ");
		if(pUser!=null){
			UpdateUser vUpdateUser = new UpdateUser(application,pUser );
			vUpdateUser.execute();
		}
		else{
			Log.i(LOG_TAG, "Error -  updateUserServ(User pUser) - pUser is null ");
		}
		Log.i(LOG_TAG, "END - updateUserServ(User pUser)");
	}

	public synchronized ApiSession getApiSession() {
		return apiSession;
	}

	public static String getLogin() {
		return login;
	}

	public static String getPassword() {
		return password;
	}

	public Application getApplication() {
		return application;
	}

	public Users getUsers() {
		return users;
	}

	public synchronized void resultGetAllUserRequest(RestGetAllUserEvent event) {
		Log.i(LOG_TAG, "START - resultGetAllUserRequest");
		Log.i(LOG_TAG, "users : " + event.getResultRequest() );
		List<User> vList = MethodeUtils.getListUserFromJsonList( event.getResultRequest() );
		for ( User vPartialUser : vList) {
			GetUser vGetUser = new GetUser(application, vPartialUser);
			vGetUser.execute();
		}
		
		Log.i(LOG_TAG, "END - resultGetAllUserRequest");		
	}

	public synchronized void resultGetUserRequest(RestGetUserEvent event) {
		Log.i(LOG_TAG, "START - resultGetUserRequest");
		Log.i(LOG_TAG, "user : " + event.getResultRequest() );
		User vuser =  MethodeUtils.getUserFromJson( event.getResultRequest() );		
		users.addUser(vuser);
		if ( vuser.getIdentifiant() != null ) {
			final 	String vId = vuser.getIdentifiant();
			Photo vPhoto = new  Photo(getApplication(), vuser );
			vPhoto.execute();
		}
	
		Log.i(LOG_TAG, "END - resultGetUserRequest");

	}



	public synchronized void resultLoginRequest(RestLoginEvent event) {
		
		
		if (!isAuthentication && !event.isFromFormView()){
			isAuthentication = event.isAuthentication() ;
			if ( isAuthentication ) {
				apiSession = event.getApiSession();
				getAllUsersByRest();				
			}
		} else if (! isAuthentication && event.isFromFormView() ) {
			isAuthentication = event.isAuthentication() ;
			if ( isAuthentication ) {
				Intent vIntent = new Intent(application, DashBoard.class);
				vIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
				application.startActivity(vIntent);
			} else {
				Toast.makeText(application, "Echec authentification", Toast.LENGTH_SHORT).show();	
			}

		}


	}


	public synchronized void resultAddUserRequest(RestAddUserEvent event) {
		Log.i(LOG_TAG, "START - resultAddUserRequest");
		Log.i(LOG_TAG, "identifiant user : " + event.getIdentifiant());
		if ( event.isAdded() ) {
			User vPartialUser = new User();
			vPartialUser.setIdentifiant(event.getIdentifiant());

			GetUser vGetUser = new GetUser(application, vPartialUser);
			vGetUser.execute();
		}		
	
		Log.i(LOG_TAG, "END - resultAddUserRequest");

	}

	public static boolean isAuthentication() {
		return isAuthentication;
	}



	public static void setAuthentication(boolean isAuthentication) {
		ServiceController.isAuthentication = isAuthentication;
	}





//	public void dissipDialog () {
//		if (progress != null ) {
//			progress.dismiss();
//		}
//		
//	}
//	
//	public void showProgressBar () {
//		progress =ProgressDialog.show(application.getBaseContext(),"Loading data", "Please wait...");
//		
//	}


}
